import OBSWebSocket from "obs-websocket-js";
import store from "../store/index"

const obs = new OBSWebSocket();
export default {
  obs: obs,
  lastFilePath: "",
  isCoonected: false,
  //连接OBS
  connect: function () {
    obs
      .connect(`ws://${store.getters.gameConfig && store.getters.gameConfig.obsAddress}:4455`, store.getters.gameConfig && store.getters.gameConfig.obsPassword)
      .then(() => {
        this.isCoonected = true;
        console.log("成功连接到obs");
      })
      .catch((err) => {
        this.isCoonected = false;
        console.error("连接obs出错:", err);
      });
  },
  //断开OBS连接
  disconnect: function () {
    obs
      .disconnect()
      .then((res) => {
        console.log("断开成功 OBS Studio", res);
      })
      .catch((err) => {
        console.log("断开失败 OBS Studio", err);
      });
  },
  //obs通讯
  obsCall: function (functionName) {
    if (!this.isCoonected) {
      console.log("未连接到OBS");
      return;
    }
    obs
      .call(functionName)
      .then((res) => {
        if (res.outputPath != undefined) {
          this.lastFilePath = res.outputPath;
          localStorage.setItem('videoPath', this.lastFilePath);
          // setTimeout(() => {
          //   this.fileLocalFile(res.outputPath);
          // }, 1000);
        }
        console.log("obsCall返回值", res);
      })
      .catch((err) => {
        console.log("obsCall错误值", err);
      });
  },
  //本地文件上传
  fileLocalFile: function (path) {
    const fileStream = fs.createReadStream(path);
    console.log("fileStream", fileStream);

    // fetch(path)
    //   .then((response) => {
    //     console.log("输出文件：", response);
    //   })
    //   .then((blob) => {
    //     console.log("输出文件流blob：", blob);
    //     // 获取文件名
    //     let temp = path?.split("/");
    //     let name = temp[temp?.length - 1];
    //     // 创建File对象
    //     let file = new File([blob], name, { type: blob.type });
    //     console.log("输出文件流file：", file);
    //   })
    //   .catch(function (error) {
    //     console.log("加载文件失败:", error);
    //   });
  },
};
